<template>
  <div>
    <!-- 教练列表页 -->
    <el-button @click="showFormDialog" type="primary">新增教练</el-button>
    <el-divider content-position="left">教练列表</el-divider>

    <!-- 表格 -->
    <el-table :data="coaches">
      <el-table-column label="教练图片" prop="coach_avatar">
        <template slot-scope="scope">
          <img :src="'http://176.221.16.8:8080'+scope.row.coach_avatar" width="60px" alt="" />
        </template>
      </el-table-column>
      <el-table-column label="教练名称" prop="coach_name"></el-table-column>
      <!-- <el-table-column label="教练电话" prop="phone"></el-table-column> -->
      <el-table-column
        label="教练健身类型"
        prop="fitness_type"
      ></el-table-column>

      <el-table-column label="操作">
        <template slot-scope="scope">
          <!--  -->
          <el-button
            @click="
              $router.push(
                `/home/plesson-list?trends_id=${scope.row.trends_id}&coach_id=${scope.row.coach_id}`
              )
            "
            size="small"
            type="info"
            icon="el-icon-view"
            circle
          ></el-button>

          <el-button
            @click="
              $router.push(`/home/coach-update?coach_id=${scope.row.coach_id}`)
            "
            size="small"
            type="warning"
            icon="el-icon-s-grid"
            circle
          ></el-button>

          <el-button
            @click="deleteCoach(scope.row.coach_id)"
            size="small"
            type="danger"
            icon="el-icon-delete"
            circle
          ></el-button>
        </template>
      </el-table-column>
    </el-table>

    <!-- 自定义对话框 -->
    <el-dialog title="新增教练" :visible.sync="dialogFormVisible">
      <el-form
      ref="form"
        style="padding-right: 40px"
        :rules="rules"
        :model="form"
        label-width="100px"
      >
        <el-form-item label="选择头像" prop="coach_avatar">
          <el-upload
            class="avatar-uploader"
            name="sbb"
            :action="`${UPLOADURL}/upload`"
            :show-file-list="false"
            :on-success="handleAvatarSuccess"
            :before-upload="beforeAvatarUpload"
          >
            <img
              v-if="form.coach_avatar"
              :src="form.coach_avatar"
              class="avatar"
            />
            <i v-else class="el-icon-plus avatar-uploader-icon"></i>
          </el-upload>
        </el-form-item>
        <el-form-item label="教练名称" prop="coach_name">
          <el-input v-model="form.coach_name" autocomplete="off"></el-input>
        </el-form-item>
        <el-form-item label="教练类型" prop="fitness_type">
          <el-select
            multiple
            v-model="form.fitness_type"
            placeholder="请选择类型"
          >
            <el-option
              v-for="item in types"
              :key="item.ft_id"
              :label="item.type"
              :value="item.type"
            >
            </el-option>
          </el-select>
        </el-form-item>
        <!-- <el-form-item label="教练电话" prop="phone">
          <el-input v-model="form.phone" autocomplete="off"></el-input>
        </el-form-item> -->
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="dialogFormVisible = false">取 消</el-button>
        <el-button type="primary" @click="submit">确 定</el-button>
      </div>
    </el-dialog>
  </div>
</template>
<!-- @click="submit" -->
<script>
import httpApi from "@/http";
export default {
  data() {
    return {
      coaches: [], //存储教练列表
      types: [], // 存储教练类型列表
      dialogFormVisible: false, // 控制是否显示表单对话框
      form: {
        // 封装表单项
        trends_id: this.$route.params.store_id, // 健身房ID
        coach_name: "",
        // phone: "",
        fitness_type: "",
        coach_avatar: "",
        coach_id: "",
      },
      rules: {
        coach_avatar: [{ required: true, message: "必填", trigger: "blur" }],
        coach_name: [{ required: true, message: "必填", trigger: "blur" }],
        fitness_type: [{ required: true, message: "必填", trigger: "blur" }],
        // phone: [{ required: true, message: "必填", trigger: "blur" }],
      },
    };
  },

  mounted() {
    this.listCoaches();
  },

  methods: {
    /** 查询教练信息 */

    /** 删除教练 */
    deleteCoach(coach_id) {
      httpApi.coachApi.delete({ coach_id }).then((res) => {
        console.log("删除的教练ID:", res);
        if (res.data.code == 200) {
          this.$message({ message: "删除成功", type: "success" });
          // 重新加载列表
          this.listCoaches();
        } else {
          this.$message({ message: "失败，请稍后再试", type: "error" });
        }
      });
    },

    /** 提交新增教练的表单 */
    submit() {
      this.$refs.form.validate((valid) => {
        if (valid) {
          // 处理请求参数中的特殊字段
          this.form.fitness_type = this.form.fitness_type.join(",");
          // 发送新增教练的请求
          httpApi.coachApi.add(this.form).then((res) => {
            if (res.data.code == 200) {
              this.$message({ message: "成功", type: "success" });
              this.dialogFormVisible = false;
              // 重新加载列表
              this.listCoaches();
            } else {
              this.$message({ message: "失败，请稍后重试", type: "error" });
            }
          });
        }
      });
    },

    /** 列出教练列表 */
    listCoaches() {
      let params = { trends_id: this.form.trends_id };
      // console.log(this.form.store_gym_id);
      httpApi.coachApi.coachList(params).then((res) => {
        console.log("加载当前教练列表", res);
        this.coaches = res.data.data;
      });
    },

    /**加载自定义对话框 */
    showFormDialog() {
      this.dialogFormVisible = true; //显示对话框
      // 加载教练类型列表，更新下拉列表框
      httpApi.coachApi.queryAllTypes().then((res) => {
        console.log("加载教练类型", res);
        this.types = res.data.data;
      });
    },

    /**上传成功后执行 */
    handleAvatarSuccess(res, file) {
      console.log(res);
      // 将上传成功的图片访问路径赋值传给form对象的属性
      this.form.coach_avatar = res.pic;
    },

    /**图片上传之前执行 */
    beforeAvatarUpload(file) {
      const isJPG = file.type === "image/jpeg";
      const isLt2M = file.size / 1024 / 1024 < 2;

      if (!isJPG) {
        this.$message.error("上传头像图片只能是 JPG 格式!");
      }
      if (!isLt2M) {
        this.$message.error("上传头像图片大小不能超过 2MB!");
      }
      return isJPG && isLt2M;
    },
  },
};
</script>

<style>
.avatar-uploader .el-upload {
  border: 1px dashed #d9d9d9;
  border-radius: 6px;
  cursor: pointer;
  position: relative;
  overflow: hidden;
}
.avatar-uploader .el-upload:hover {
  border-color: #409eff;
}
.avatar-uploader-icon {
  font-size: 28px;
  color: #8c939d;
  width: 178px;
  height: 178px;
  line-height: 178px;
  text-align: center;
}
.avatar {
  height: 178px;
  display: block;
}
</style>
